package com.wzy.netty.nio;


import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 描述
 * 创建人    Wzy
 * 创建时间: 2020-05-20  13:52
 */
public class NIOFileChannel {
    public static void main(String[] args) throws IOException {
        String str = "hello,world";

        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");

        //获取channel, 包装 输出流
        /*channel真是类型 是 fileChannel*/
        FileChannel fileChannel = fileOutputStream.getChannel();

        /*创建byteBuffer*/
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());

        /*反转buffer 重置positoin*/
        byteBuffer.flip();

        //bytebuffer写入channel
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(format);
    }
}
